import api from '@/api/index'

const eventTypeMap = {
    1: '预警',
    2: '故障',
    3: '火警'
}
let deviceDefObj = {}
let handleGetDef = () => {
    return new Promise((resolve, reject) => {
        api
            .post('/index/DeviceDefault/getDef')
            .then((res) => {
                resolve(res.data)
            })
            .catch((e) => {
                console.log(e)
            })
    })
}
export const setDeviceData = (list, method) => {
    if (!list || !Array.isArray(list)) return false
    return new Promise(async (resolve, reject) => {
        if (!Object.keys(deviceDefObj).length) {
            deviceDefObj = await handleGetDef()
            console.log(deviceDefObj, 'deviceDefObj')
        }

        for (let item of list) {
            item.device_data = item.device_data ? JSON.parse(JSON.stringify(item.device_data)) : item.last_report_data
            item.measure = ''
            if (item?.last_report_data?.orgId) {
                let _measure = ''
                //电压
                if (item.last_report_data.voltage) {
                    _measure = '电压：' + item.last_report_data.voltage + ' V<br />'
                    item.measure += _measure
                }
                //烟雾浓度
                if (item.last_report_data.smokeStrength) {
                    _measure = '烟雾浓度：' + item.last_report_data.smokeStrength + ' dB/M<br />'
                    item.measure += _measure
                }
                //液位
                if (item.last_report_data.liquidLeve) {
                    _measure = '液位：' + item.last_report_data.liquidLeve + ' M<br />'
                    item.measure += _measure
                }
                //设备信号强度
                if (item.last_report_data.RSSI) {
                    _measure = '设备信号强度：' + item.last_report_data.RSSI + ' dbm<br />'
                    item.measure += _measure
                }
                //设备压力值
                if (item.last_report_data.Mpa) {
                    _measure = '设备压力值：' + item.last_report_data.Mpa + ' Mpa<br />'
                    item.measure += _measure
                }
                //可燃气体浓度
                if (item.last_report_data.gasStrength) {
                    _measure = '可燃气体浓度：' + item.last_report_data.gasStrength + ' LEL<br />'
                    item.measure += _measure
                }
                //温度
                if (item.last_report_data.temp) {
                    _measure = '温度：' + item.last_report_data.temp + ' °C<br />'
                    item.measure += _measure
                }
                //漏电率
                if (item.last_report_data.leakageRate) {
                    _measure = '温度：' + item.last_report_data.leakageRate + ' %<br />'
                    item.measure += _measure
                }
                //电流毫安
                if (item.last_report_data.electricity) {
                    _measure = '温度：' + item.last_report_data.electricity + ' mA<br />'
                    item.measure += _measure
                }
            } else {
                if (!item.device_data && item.log_data) item.device_data = item.log_data
                if (!item.device_data && item.last_report_data) item.device_data = item.last_report_data
                let curDeviceData = item.device_data
                item.event_type = eventTypeMap[item.method]
                for (let subitem in deviceDefObj) {
                    console.log(subitem)
                    if (subitem != item.device_method || Object.prototype.toString.call(item.device_data) !== '[object Object]')
                        continue
                    for (let granditem in deviceDefObj[subitem]) {
                        if (curDeviceData.service_type != granditem) continue
                        for (let greatgrand in deviceDefObj[subitem][granditem]) {
                            for (let lastitem in curDeviceData) {
                                if (lastitem != greatgrand) continue
                                let _greatgrand = deviceDefObj[subitem][granditem][greatgrand] || {}
                                //量值

                                if (_greatgrand.is_show === 1) {
                                    let _name = _greatgrand.name //名称
                                    if (_name === '量值') _name = ''
                                    let _unit = _greatgrand.unit || '' //单位
                                    let _measure = (_name ? `${_name}:` : '') + `${curDeviceData[lastitem]}${_unit}<br />`
                                    item.measure += _measure
                                }
                            }
                        }
                    }
                }
            }

            item.pos_no = item.device_data?.sub_device_no || item.device_no
            item.new_address = item.device_data.sub_address || item.address || ''
            item.new_mold_name = item.device_data.sub_name || item.device_data.unitTypeName || item.mold_name
        }
        resolve(list)
    })
}
